using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using SwinMessengerLibrary;

namespace SwinMessenger
{
    public partial class ContactPanel : UserControl
    {
        private bool _IsShiftDown;
        Contact _Contact;
        Form _Container;

        public event EventHandler OnMessaging;

        public ContactPanel(Form container, Contact contact)
            : this()
        {
            _Container = container;
            _Contact = contact;

            _Contact.CanClose = false;

            _Contact.OnMessage += new EventHandler(_Contact_OnMessage);

            SendButton.Enabled = false;

        }

        void _Contact_OnMessage(object sender, EventArgs e)
        {
            if (InvokeRequired)
            {
                BeginInvoke(new EventHandler(_Contact_OnMessage),
                    new object[] { this, e });
                return;
            }
            if (e is MessageEventArgs)
            {
                MessageEventArgs mea = (MessageEventArgs)e;
                string id = mea.Id;
                string message = mea.Message;
                if (message != null)
                {
                    DisplayMessage(id, message);
                    if (OnMessaging != null)
                    {
                        OnMessaging(this, mea);
                    }
                }
                else
                {
                    if (OnMessaging != null)
                    {
                        OnMessaging(this, mea);
                    }
                }
            }
        }

        protected ContactPanel()
        {
            InitializeComponent();
        }

        private void SendButton_Click(object sender, EventArgs e)
        {
            if (SendButton.Enabled)
            {
                _Contact.SendTo(SendTextBox.Text);

                DisplayMessage("Me", SendTextBox.Text);

                SendTextBox.Text = "";
                SendButton.Enabled = false;
            }
        }

        private void SendTextBox_TextChanged(object sender, EventArgs e)
        {
            if (!SendTextBox.Text.Equals(""))
            {
                SendButton.Enabled = true;
            }
        }

        private void DisplayMessage(string id, string message)
        {
            DisplayTextBox.Text += id + " " + DateTime.Now + ":\r\n";
            DisplayTextBox.Text += message;
            DisplayTextBox.Text += "\r\n\r\n";
        }

        private void DisplayTextBox_TextChanged(object sender, EventArgs e)
        {
            DisplayTextBox.SelectionStart = DisplayTextBox.Text.Length;
            DisplayTextBox.ScrollToCaret();
        }

        private void SendTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter && e.Modifiers == Keys.None)
            {
                _IsShiftDown = true;
            }

        }

        private void SendTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (_IsShiftDown)
            {
                e.Handled = true;
                SendButton_Click(this, EventArgs.Empty);
                _IsShiftDown = false;
            }
        }
    }
}
